

jQuery(function ($) {
    var user = JSON.parse(localStorage.getItem('user_info')) || {};
    var user_id = user.id;
    $('.coupon_box_right button').click(function () {
        if (!user_id) {
            alert('您还没有登录，请先登录！');
            return;
        }
        var _this = $(this);
        var coupon_id = $(this).attr('data-coupon-id');
        var param = {
            coupon_id: coupon_id,
            user_id: user_id
        };
        send_ajax('post', '/api/coupon/get', param, function callback(res) {
            if (res.code == 200) {
                $(_this).attr("disabled", "disabled");
                $(_this).parents('.coupon_box').find('i').show();
            }
        });
    });
    $('.coupon_content_right').click(function () {
        if (!user_id) {
            alert('您还没有登录，请先登录！');return;
        }
        var _this = $(this);
        var coupon_id = $(this).attr('data-coupon-id');
        var param = {
            coupon_id: coupon_id,
            user_id: user_id
        };
        send_ajax('post', '/api/coupon/get', param, function callback(res) {
            if (res.code == 200) {
                $(_this).unbind('click');
                $(_this).parents('li').find('i').show();
            }
        });
    });
    if (user_id) {
        var couponBox = $('.coupon_box_right button');

        var _loop = function _loop(k) {
            couponId = $(couponBox[k]).attr('data-coupon-id');
            param = {
                "UserCoupon": {
                    "user_id": user_id,
                    "coupon_id": couponId,
                    "unable_flag": 0
                }
            };

            send_ajax('post', '/head', param, function callback(res) {
                if (res.UserCoupon.code == 200 && res.UserCoupon.count > 0) {
                    $(couponBox[k]).parents('.coupon_box').find('i').show();
                }
            });
        };

        for (var k = 0; k < couponBox.length; k++) {
            var couponId;
            var param;

            _loop(k);
        };
        var couponElement = $('.coupon_content_right');

        var _loop2 = function _loop2(i) {
            couponId = $(couponElement[i]).attr('data-coupon-id');
            param = {
                "UserCoupon": {
                    "user_id": user_id,
                    "coupon_id": couponId,
                    "unable_flag": 0
                }
            };

            send_ajax('post', '/head', param, function callback(res) {
                if (res.UserCoupon.code == 200 && res.UserCoupon.count > 0) {
                    $(couponElement[i]).parents('li').find('i').show();
                };
            });
        };

        for (var i = 0; i < couponElement.length; i++) {
            var couponId;
            var param;

            _loop2(i);
        };
    };
});